﻿using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OpenAuth.App;
using OpenAuth.App.Request;
using System.IO;
using System.Text;
using OpenAuth.WebApi.Model.Auditing;
using System.Reflection;

namespace OpenAuth.WebApi.Model
{
    public class GlobalActionFilter : ActionFilterAttribute
    {
        private readonly ApiLogApp _apiApp;

        public GlobalActionFilter(ApiLogApp apiApp)
        {
            _apiApp = apiApp;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            AuditingHelper auditingHelper = new AuditingHelper();
            AddOrUpdateApiLogReq req = new AddOrUpdateApiLogReq();
           
            req.ApiName = filterContext.ActionDescriptor.AsControllerActionDescriptor().MethodInfo.Name;
            req.ApiType = filterContext.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType() != null
                    ? filterContext.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType().FullName
                    : "";
            req.ReqBody = auditingHelper.ConvertArgumentsToJson(filterContext.ActionArguments);
         
            req.EditTime = DateTime.Now.ToString("yyyyMMdd hh:mm:ss");
            //_apiApp.Add(req);

            //--------------------------------
           

        }
    }
}
